
const lcT = {

  
  // /* 延迟定时器 */
  // setTimeout(time, [func]) {
  //   setTimeout(() => {
  //     func();
  //   }, time)
  // },
  /* 点击节流 */
  clickAntiShake({ _this, time }) {
    if (_this.timer) {
      clearTimeout(_this.timer);
      _this.timer = setTimeout(() => {
        _this.allowClick = true;
      }, time);
    }
    if (_this.allowClick == true) {
      _this.allowClick = false;
      _this.timer = setTimeout(() => {
        _this.allowClick = true;
      }, time);
      return true;
    } else {
      // _this.tools.base.Toast({
      //   msg: "不要频繁点击",
      // });
      return false;
    }

    /* 
      //防抖
      allowClick: true, //允许点击
      timer: "",  //计时器

       //通过标识
      var isPass = true;
      //防抖
      isPass = this.tools.clickAntiShake({
        _this: this,
        time: 3000,
      });
      if(!isPass) return;

    */
  },


  /* 从链接中保存Lo信息 */
  saveLoFromUrl({ _this, name }) {
    lcT.checkArgm({
      _this, name
    })
    let value = _this.$route.query[name];
    if (value) {
      lcT.setLo(name, value);
    }
    /* 
      this.tools.saveLoFromUrl({
        _this: this,
        name: "inviteCode",
      });
    */
  },

  /* 获取信息并删除 */
  getLoMsgAndDel(name) {
    lcT.checkArgm({
      name
    });
    let loData = lcT.getLo(name);
    lcT.removeLo(name);
    return loData;
    /* 
        this.tools.getLoMsgAndDel("inviteCode");
    */
  },

  /* 从链接中保存Se信息 */
  saveSeFromUrl({ _this, name }) {
    lcT.checkArgm({
      _this, name
    })
    let value = _this.$route.query[name];
    if (value) {
      lcT.setSe(name, value);
    }
    /* 
      this.tools.saveSeFromUrl({
        _this: this,
        name: "inviteCode",
      });
    */
  },

  /* 设置回跳路径 */
  setCBPath() {
    lcT.setSe("cbPath", window.location.href);
  },

  /* 获取Se信息并删除 */
  getSeMsgAndDel(name) {
    lcT.checkArgm({
      name
    });
    let loData = lcT.getSe(name);
    lcT.removeSe(name);
    return loData;
    /* 
        this.tools.getSeMsgAndDel("inviteCode");
    */
  },

  //参数校验
  checkArgm(pa) {
    for (var itemKey in pa) {
      if (!pa[itemKey]) {
        console.log(itemKey + "不能为空");
      }
    }
  },

  /* 检查登录 */
  checkLogin() {
    var token = localStorage.getItem("token");
    if (token) {
      return true;
    } else {
      return false;
    }
  },

  /* 保存session */
  setSe(name, str) {
    sessionStorage.setItem(name, str);
  },

  /* 获取session */
  getSe(name) {
    return sessionStorage.getItem(name);
  },

  removeSe(name) {
    lcT.checkArgm({ name });
    sessionStorage.removeItem(name);
    return true;
  },

  
  /* 获取对象数据session */
  getSeObj(name) {
    var objStr = sessionStorage.getItem(name);
    return JSON.parse(objStr);
  },

  /* 保存对象数据session */
  setSeObj(name, obj) {
    var objStr = JSON.stringify(obj);
    sessionStorage.setItem(name, objStr);
  },




  /* 保存对象数据local */
  setLo(name, str) {
    localStorage.setItem(name, str);
  },

  /* 获取对象数据session */
  getLo(name) {
    var str = localStorage.getItem(name);
    return str;
  },

  removeLo(name) {
    lcT.checkArgm({ name });
    localStorage.removeItem(name);
    return true;
  },


  /* 保存对象数据local */
  setLoObj(name, obj) {
    var objStr = JSON.stringify(obj);
    localStorage.setItem(name, objStr);
  },

  /* 获取对象数据session */
  getLoObj(name) {
    var objStr = localStorage.getItem(name);
    return JSON.parse(objStr);
  },



  /* 非空校验 */
  checkInput({ formData = {}, checkData = {} }) {
    for (var itemKey in checkData) {
      if (!formData[itemKey]) {
        jhTools.base.Toast({
          message: checkData[itemKey] + "不能为空!"
        });
        return false;
      }
    }
    return true;
    /* 
      //非空
      isPass = checkInput ({
        formData: this.formData,
        checkData: {
          mobile: "手机账号",  //账号
          password: "密码", //密码
          cPwd: "确认密码",     //确认密码
          code: "验证码",     //验证码
        },
      })
      if(!isPass) return;
    */
  },

  /* 倒计时 */
  countDownF({ timeS = 60, cb }) {
    let isPass = true;
    lcT.checkOptF({ timeS, cb });
    if (!isPass) return;
  },

  /* 参数校验 */
  checkOptF(opt) {
    for (var itemKey in opt) {
      if (!opt[itemKey]) {
        console.log(opt[itemKey] + "为必传!!");
        return false;
      }
    }
    return true;
    /* 
      let isPass = true; 
      lcT.checkOptF({timeS = 60, cb});
      if(!isPass) return;
    */
  },

  /* 数字校验 */
  checkNum({ str, tips, inLen, len, inputData, tipsData, lenData, isCheckNum = true }) {
    let isPass = true;
    if (isCheckNum == true || isCheckNum == 'true') {
      isCheckNum = true;
    } else if (isCheckNum == false || isCheckNum == 'false') {
      isCheckNum = false;
    }
    if (str) {
      //数字校验
      if (isCheckNum && (Number(str) + "") == "NaN") {
        jhTools.base.Toast({
          msg: tips + "必须为数字",
        });
        isPass = false;
        return isPass;
      }
      if (len != undefined) {
        if (str.length !== len) {
          jhTools.base.Toast({
            msg: tips + "长度需等于" + len,
          });
          isPass = false;
          return isPass;
        }
      } else if (inLen) {
        if (str.length < inLen[0]) {
          jhTools.base.Toast({
            msg: tips + "长度需小于" + inLen[0],
          });
          isPass = false;
          return isPass;
        }
        if (str.length > inLen[1]) {
          jhTools.base.Toast({
            msg: tips + "长度需小于" + inLen[1],
          });
          isPass = false;
          return isPass;
        }
      }
      return isPass;

    } else if (inputData) {
      for (let itemKey in tipsData) {
        //数字校验
        if (isCheckNum && (Number(inputData[itemKey]) + "") == "NaN") {
          console.log(itemKey, Number(inputData[itemKey]) + "");
          jhTools.base.Toast({
            msg: tipsData[itemKey] + "必须为数字",
          });
          isPass = false;
          return isPass;
        }
        //长度限制
        if (lenData) {
          var lenItem = lenData[itemKey];
          if (lenItem.len != undefined) {
            if (lenItem.len != inputData[itemKey].length) {
              jhTools.base.Toast({
                msg: tipsData[itemKey] + "长度需等于" + lenItem.len,
              });
              isPass = false;
              return isPass;

            }
          } else if (lenItem.inLen && lenItem.inLen.length > 0) {
            if (inputData[itemKey].length < lenItem.inLen[0]) {
              jhTools.base.Toast({
                msg: tipsData[itemKey] + "长度需小于" + lenItem.inLen[0],
              });
              isPass = false;
              return isPass;
            }
            if (inputData[itemKey].length > lenItem.inLen[1]) {
              jhTools.base.Toast({
                msg: tipsData[itemKey] + "长度需小于" + lenItem.inLen[1],
              });
              isPass = false;
              return isPass;
            }
          }
        }
      }
      return isPass;
    }
    // //数字
    // isPass = this.tools.checkNum({
    //   // isCheckNum: true,
    //   // str: this.inviteCode,
    //   // tips: "邀请码",
    //   // inLen: [1, 6], // 范围长度限制
    //   // len: 6, //等于长度限制 优先级高

    //   inputData: {
    //     inviteCode: this.inviteCode,
    //     code: this.code,
    //   },
    //   tipsData: {
    //     inviteCode: "邀请码",
    //     code: "验证码",
    //   },
    //   lenData: {
    //     inviteCode: {
    //       // inLen: [1,4],
    //       len: 6
    //     },
    //     code: {
    //       // inLen: [1,3],
    //       len: 6
    //     },
    //   },
    // });
    // if(!isPass) return;
  },


  /* 保存当前回调路径 */
  setCBPath() {
    lcT.setSe("cbPath", window.location.href);
    /* 
      this.tools.setCBPath({ "cbPath": ""});
    */
  },

  /* 清除 */
  clearCBPath() {
    this.tools.setSe("cbPath", "");
  },


  /* 切换密码隐藏 */
  switchPwd({ refName, el, _this }) {
    let type;
    type = _this.$ref[refName].type;
    return;
    _this.$ref[refName].type = "text"
  },



  /* 延时设置pwd属性 不显示默认密码 */
  setPwd({ refNameArr, _this }) {
    setTimeout(() => {
      refNameArr.forEach((item, index) => {
        let el = _this.$refs[item];
        el.type = 'password';
      })
    }, 500);
  },



  /* 确认密码 */
  comfirmPwd({ pwd, cPwd }) {
    if (pwd == cPwd) {
      return true;
    } else {
      jhTools.base.Toast({
        msg: "两次输入密码不一致"
      })
      return false;
    }
  },



  /* 国内手机号校验 */
  checkPhone(phone) {
    let isPass = true;
    phone = phone + '';
    if (!/^1[3456789]\d{9}$/.test(phone)) {
      isPass = false;
    }
    if ((Number(phone) + "") == "NaN") {
      isPass = false;
    }
    if (!isPass) {
      jhTools.base.Toast({
        message: "手机号格式不正确"
      })
    }
    return isPass;
    /* 
      
      isPass = this.tools.checkTelInter(this.formData.mobile);
      if(isPass) return;s
    */
  },

  /* 国际手机号校验 */
  checkTelInter(phone) {
    let isPass = true;
    phone = phone + '';
    // if (!/^1[3456789]\d{9}$/.test(phone)) {
    //   isPass = false;
    // }
    if ((Number(phone) + "") == "NaN") {
      isPass = false;
    }
    if (!isPass) {
      jhTools.base.Toast({
        message: "手机号格式不正确"
      })
    }
    return isPass;
  },

  // 检查电话格式
  checkPhoneNum(phone) {
    phone = phone + '';
    return /^1[3456789]\d{9}$/.test(phone);
  },

  /* 返回随机数 */
  getRandomStr (){
    return ( Math.random() * 100 ).toFixed(4);
    /* 
      //更新页面
      _this.showPageKey = _this.tools.getRandomStr();
    */
  },

}

export default lcT;